package com.esri.core.map.ogc.kml;

import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Point;
import com.esri.core.internal.util.d;
import io.reactivex.annotations.SchedulerSupport;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public class KmlNode {
    private boolean a;
    private String b;
    private boolean c;
    private String d;
    private Envelope e;
    private Envelope f;
    private long g;
    private long h;
    private Type i;
    private GraphicType j;
    private boolean k;
    private NodeIcon l;
    private int m;
    private boolean n = false;
    private boolean o = false;
    private boolean p = false;
    private boolean q = false;
    private int r = 0;
    private int s = 0;
    private BalloonStyle t = new BalloonStyle();

    private static final int a(int i, int i2, int i3, int i4) {
        return (i << 24) | (i2 << 16) | (i3 << 8) | i4;
    }

    private native String nativeGetChildNodes(long j);

    private native double[] nativeGetDefaultExtent(long j);

    private native int[] nativeGetIconData(long j);

    private native String nativeGetIconMetaData(long j);

    private native double[] nativeGetProjectedExtent(long j);

    private native boolean nativeIsHighlight(long j);

    private native void nativeSetNodeVisibility(long j, boolean z);

    /* loaded from: classes.dex */
    public enum Type {
        UNKNOWN("Unknown"),
        DOCUMENT("Document"),
        FOLDER("Folder"),
        NETWORKLINK("Networklink"),
        PLACEMARK("Placemark"),
        PHOTOOVERLAY("PhotoOverlay"),
        GROUNDOVERLAY("GroudOverlay"),
        SCREENOVERLAY("ScreenOverlay"),
        TOUR("Tour");
        
        private String a;

        Type(String str) {
            this.a = str;
        }

        @Override // java.lang.Enum
        public String toString() {
            return this.a;
        }
    }

    /* loaded from: classes.dex */
    public enum GraphicType {
        NONE(SchedulerSupport.NONE),
        POINT("point"),
        POLYLINE("polyline"),
        POLYGON("polygon"),
        EXTRUDED_POINT("extruded_point"),
        EXTRUDED_POLYLINE("extruded_polyline"),
        EXTRUDED_POLYGON("extruded_polygon"),
        MODEL("model"),
        MIXED("mixed");
        
        private String a;

        GraphicType(String str) {
            this.a = str;
        }

        @Override // java.lang.Enum
        public String toString() {
            return this.a;
        }
    }

    /* loaded from: classes.dex */
    public enum ListItemType {
        CHECK("check"),
        RADIO_FOLDER("radioFolder"),
        CHECK_OFF_ONLY("checkOffOnly"),
        CHECK_HIDE_CHILDREN("checkHideChildren");
        
        private String a;

        ListItemType(String str) {
            this.a = str;
        }

        @Override // java.lang.Enum
        public String toString() {
            return this.a;
        }
    }

    /* loaded from: classes.dex */
    public class NodeIcon {
        int a;
        int b;
        int[] c;
        boolean d = false;

        public NodeIcon() {
        }

        public int getWidth() {
            return this.a;
        }

        public int getHeight() {
            return this.b;
        }

        public int[] getData() {
            return this.c;
        }

        public String toString() {
            return "NodeIcon [Width=" + this.a + ",Height=" + this.b + "]";
        }
    }

    public boolean hasChildNode() {
        return this.k;
    }

    public int getChildNodesCount() {
        return this.m;
    }

    public List<KmlNode> getChildNodes() {
        if (this.g != 0 && hasChildNode()) {
            return a();
        }
        return Collections.emptyList();
    }

    public long getId() {
        return this.h;
    }

    public long getHandle() {
        return this.g;
    }

    public boolean isVisible() {
        return this.a;
    }

    public boolean isPartiallyVisible() {
        return this.q;
    }

    public void setVisible(boolean z) {
        this.a = z;
        long j = this.g;
        if (j != 0) {
            nativeSetNodeVisibility(j, z);
        }
    }

    public String getName() {
        return this.b;
    }

    public boolean isSelected() {
        long j = this.g;
        if (j != 0) {
            this.c = nativeIsHighlight(j);
        }
        return this.c;
    }

    public BalloonStyle getBalloonStyle() {
        return this.t;
    }

    public Type getType() {
        return this.i;
    }

    public GraphicType getGraphicType() {
        return this.j;
    }

    public Point getCenter() {
        double[] nativeGetProjectedExtent;
        if (this.e == null && (nativeGetProjectedExtent = nativeGetProjectedExtent(this.g)) != null && nativeGetProjectedExtent.length == 4) {
            this.e = new Envelope(nativeGetProjectedExtent[0], nativeGetProjectedExtent[1], nativeGetProjectedExtent[2], nativeGetProjectedExtent[3]);
        }
        Envelope envelope = this.e;
        if (envelope != null) {
            return envelope.getCenter();
        }
        return null;
    }

    public Envelope getExtent() {
        double[] nativeGetProjectedExtent;
        if (this.e == null && (nativeGetProjectedExtent = nativeGetProjectedExtent(this.g)) != null && nativeGetProjectedExtent.length == 4) {
            this.e = new Envelope(nativeGetProjectedExtent[0], nativeGetProjectedExtent[1], nativeGetProjectedExtent[2], nativeGetProjectedExtent[3]);
        }
        return this.e;
    }

    public Envelope getDefaultExtent() {
        double[] nativeGetDefaultExtent;
        if (this.f == null && (nativeGetDefaultExtent = nativeGetDefaultExtent(this.g)) != null && nativeGetDefaultExtent.length == 4) {
            this.f = new Envelope(nativeGetDefaultExtent[0], nativeGetDefaultExtent[1], nativeGetDefaultExtent[2], nativeGetDefaultExtent[3]);
        }
        return this.f;
    }

    public ListItemType getListItemType() {
        if (this.o) {
            return ListItemType.CHECK_OFF_ONLY;
        }
        if (this.p) {
            return ListItemType.RADIO_FOLDER;
        }
        if (this.n) {
            return ListItemType.CHECK_HIDE_CHILDREN;
        }
        return ListItemType.CHECK;
    }

    public NodeIcon getIcon() {
        String[] split;
        if (this.l == null) {
            this.l = new NodeIcon();
            String nativeGetIconMetaData = nativeGetIconMetaData(this.g);
            if (nativeGetIconMetaData == null || (split = nativeGetIconMetaData.split(",")) == null || split.length != 3) {
                return null;
            }
            this.l.a = Integer.valueOf(split[0]).intValue();
            this.l.b = Integer.valueOf(split[1]).intValue();
            this.l.d = Integer.valueOf(split[0]).intValue() == 1;
            this.l.c = nativeGetIconData(this.g);
            for (int i = 0; i < this.l.a * this.l.b; i++) {
                this.l.c[i] = a(this.l.c[i]);
            }
        }
        return this.l;
    }

    public int getColor() {
        return this.s;
    }

    public static KmlNode fromJson(JsonParser jsonParser) throws Exception {
        if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT) {
            return null;
        }
        KmlNode kmlNode = new KmlNode();
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String currentName = jsonParser.getCurrentName();
            jsonParser.nextToken();
            if ("handler".equals(currentName)) {
                kmlNode.g = jsonParser.getLongValue();
            } else if ("id".equals(currentName)) {
                kmlNode.h = jsonParser.getLongValue();
            } else if ("name".equals(currentName)) {
                kmlNode.b = jsonParser.getText();
            } else if ("popup".equals(currentName)) {
                kmlNode.d = jsonParser.getText();
            } else {
                if ("visible".equals(currentName)) {
                    kmlNode.a = jsonParser.getIntValue() != 0;
                } else if ("highlight".equals(currentName)) {
                    kmlNode.c = jsonParser.getIntValue() != 0;
                } else if ("radioFolder".equals(currentName)) {
                    kmlNode.p = jsonParser.getIntValue() != 0;
                } else if ("checkOffOnly".equals(currentName)) {
                    kmlNode.o = jsonParser.getIntValue() != 0;
                } else if ("checkHideChildren".equals(currentName)) {
                    kmlNode.n = jsonParser.getIntValue() != 0;
                } else if ("partialVisibility".equals(currentName)) {
                    kmlNode.q = jsonParser.getIntValue() != 0;
                } else if ("nodeColor".equals(currentName)) {
                    kmlNode.s = a(jsonParser.getIntValue());
                } else if ("popupColor".equals(currentName)) {
                    kmlNode.r = a(jsonParser.getIntValue());
                } else if ("childCount".equals(currentName)) {
                    int intValue = jsonParser.getIntValue();
                    kmlNode.m = intValue;
                    kmlNode.k = intValue > 0;
                } else if ("type".equals(currentName)) {
                    kmlNode.i = Type.valueOf(jsonParser.getText().toUpperCase());
                } else if ("graphicType".equals(currentName)) {
                    kmlNode.j = GraphicType.valueOf(jsonParser.getText().toUpperCase());
                } else {
                    jsonParser.skipChildren();
                }
            }
        }
        return kmlNode;
    }

    private static final int a(int i) {
        return a((i >> 24) & 255, i & 255, (i >> 8) & 255, (i >> 16) & 255);
    }

    private List<KmlNode> a() {
        long j = this.g;
        if (j != 0) {
            try {
                return a(nativeGetChildNodes(j));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return Collections.emptyList();
    }

    public String toString() {
        return "KmlNode [mVisible=" + this.a + ", mName=" + this.b + ", mHighlight=" + this.c + ", mBalloon_popup=" + this.d + ", mProjectedExtent=" + this.e + ", mNativeEnvelope=" + this.f + ", mNodeHandler=" + this.g + ", mType=" + this.i + ", mGraphicType=" + this.j + ", mHasChildNode=" + this.k + ", mNodeIcon=" + this.l + ", mChildNodeCount=" + this.m + ", mIsPartialVisibility=" + this.q + ", mPopupColor=" + this.r + ", mBalloonStyle=" + this.t + "]";
    }

    private static List<KmlNode> a(String str) throws Exception {
        JsonParser jsonParser;
        try {
            jsonParser = d.c(str);
        } catch (Throwable th) {
            th = th;
            jsonParser = null;
        }
        try {
            jsonParser.nextToken();
            if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                ArrayList arrayList = new ArrayList();
                while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                    KmlNode fromJson = fromJson(jsonParser);
                    if (fromJson != null) {
                        arrayList.add(fromJson);
                    }
                }
                if (jsonParser != null) {
                    jsonParser.close();
                }
                return arrayList;
            }
            if (jsonParser != null) {
                jsonParser.close();
            }
            return Collections.emptyList();
        } catch (Throwable th2) {
            th = th2;
            if (jsonParser != null) {
                jsonParser.close();
            }
            throw th;
        }
    }

    /* loaded from: classes.dex */
    public class BalloonStyle {
        public BalloonStyle() {
        }

        public int getBackgroundColor() {
            return KmlNode.this.r;
        }

        public String getFormattedText() {
            return KmlNode.this.d;
        }
    }
}
